package trabajoISW2.presentation.server;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import trabajoISW2.data.BookDAO;
import trabajoISW2.data.IBookDAO;
import trabajoISW2.data.IOrderDAO;
import trabajoISW2.data.OrderDAO;
import trabajoISW2.domain.implementations.Book;
import trabajoISW2.domain.implementations.Cart;
import trabajoISW2.domain.implementations.Detail;
import trabajoISW2.domain.implementations.Order;
import trabajoISW2.presentation.client.CartManagementUseCase;
import trabajoISW2.presentation.shared.BookDTO;
import trabajoISW2.presentation.shared.BookMapper;
import trabajoISW2.presentation.shared.CartDTO;
import trabajoISW2.presentation.shared.DetailDTO;
import trabajoISW2.presentation.shared.OrderDTO;
import trabajoISW2.presentation.shared.OrderMapper;

public class CartManagement implements
		CartManagementUseCase {
	private IBookDAO bDao = new BookDAO();
	private IOrderDAO oDao = new OrderDAO();

	@Override
	public CartDTO addBook(CartDTO cart, String isbn, int quantity) {
		if (cart == null)
			cart = new CartDTO(new LinkedList<DetailDTO>(), 0);
		
		Book b = bDao.getBook(isbn);
		
		if (b == null)
			System.err.println("There is no book with ISBN " + isbn);
		else {
			BookDTO bdto = BookMapper.createDTO(b);
			DetailDTO d = new DetailDTO(bdto, quantity);
			cart.getDetails().add(d);
			cart.setAmount(cart.getAmount()
					+ (d.getQuantity() * d.getBook().getPrice()));
		}

		return cart;

	}

	@Override
	public CartDTO modifyDetail(CartDTO cart, int detailId, int quantity) {

		if (cart == null)
			System.err.println("There is no cart");
		else {
			if (detailId <= 0 || detailId > cart.getDetails().size())
				System.err.println("There is no detail with id " + detailId);
			else if (quantity < 0 || quantity > 99)
				System.err
						.println("Select a correct quantity. Min: 1. Max 100");
			else {
				DetailDTO d = cart.getDetails().get(detailId - 1);
				d.setQuantity(quantity);
			}
		}

		return cart;
	}

	@Override
	public CartDTO removeBook(CartDTO cart, int detailId) {

		if (cart == null)
			System.err.println("There is no cart");
		else if (detailId <= 0 || detailId > cart.getDetails().size())
			System.err.println("There is no detail with id" + detailId);
		else{
			cart.getDetails().remove(detailId - 1);
		}
		return cart;

	}

	@Override
	public OrderDTO payCart(CartDTO cart) {
		OrderDTO odto = null;
		if (cart == null)
			System.err.println("There is no cart");
		else if (cart.getDetails().isEmpty())
			System.err.println("Your cart is empty");
		else {
			odto = new OrderDTO(cart);
			List<Detail> details = new LinkedList<Detail>();
			Iterator<DetailDTO> it = cart.getDetails().iterator();
			DetailDTO ddto;
			Detail d;
			Book b;
			int i = 1;
			while (it.hasNext()){
				
				ddto = it.next();
				b = bDao.getBook(ddto.getBook().getIsbn());
				d = new Detail(i,ddto.getQuantity(), b);
				details.add(d);
				i++;
			}
			Cart c = new Cart(details);
			Order order = OrderMapper.createDomainObject(null, null, c);
			Order o = oDao.createOrder(order);
			odto.setOrderId(o.getOrderId());

		}
		return odto;
	}

}
